"""
记账相关的 API 路由
"""

from datetime import date

from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.ext.asyncio import AsyncSession

from src.auth import get_current_family_member
from src.crud.accounting_crud import (
    create_account,
    create_category,
    create_project,
    create_transaction,
    delete_account,
    delete_category,
    delete_project,
    delete_transaction,
    get_account_by_id,
    get_account_statistics,
    # 账户相关
    get_accounts,
    get_all_categories,
    # 分类相关
    get_categories,
    get_category_by_id,
    # 统计相关
    get_category_statistics,
    get_project_by_id,
    get_project_statistics,
    get_projects,
    get_transaction_by_id,
    get_transaction_summary,
    # 交易记录相关
    get_transactions,
    update_account,
    update_category,
    update_project,
    update_transaction,
)
from src.database import get_db
from src.models.accounting_model import TransactionType
from src.models.family_model import FamilyMember
from src.schemas.accounting_schema import (
    # 账户相关
    AccountCreate,
    AccountResponse,
    AccountSummary,
    AccountUpdate,
    # 批量操作
    BatchCategoryCreate,
    BatchTransactionCreate,
    # 分类相关
    CategoryCreate,
    CategoryResponse,
    CategorySummary,
    CategoryUpdate,
    CategoryWithChildren,
    # 项目相关
    ProjectCreate,
    ProjectResponse,
    ProjectSummary,
    ProjectUpdate,
    # 交易记录相关
    TransactionCreate,
    TransactionList,
    TransactionResponse,
    # 统计相关
    TransactionSummary,
    TransactionUpdate,
)

router = APIRouter(prefix="/accounting", tags=["accounting"])


# 分类相关的路由
@router.post(
    "/categories", response_model=CategoryResponse, status_code=201, summary="创建分类"
)
async def create_category_endpoint(
    category: CategoryCreate,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """创建新的记账分类"""
    # 如果指定了父分类,检查父分类是否存在
    if category.parent_id:
        parent_category = await get_category_by_id(
            db, category.parent_id, current_user.family_id
        )
        if not parent_category:
            raise HTTPException(status_code=404, detail="父分类不存在")

    db_category = await create_category(
        db=db,
        name=category.name,
        family_id=current_user.family_id,
        parent_id=category.parent_id,
        icon=category.icon,
        color=category.color,
        sort_order=category.sort_order,
    )
    return db_category


@router.post(
    "/categories/batch",
    response_model=list[CategoryResponse],
    status_code=201,
    summary="批量创建分类",
)
async def batch_create_categories(
    batch_data: BatchCategoryCreate,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """批量创建记账分类"""
    created_categories = []

    for category_data in batch_data.categories:
        # 如果指定了父分类,检查父分类是否存在
        if category_data.parent_id:
            parent_category = await get_category_by_id(
                db, category_data.parent_id, current_user.family_id
            )
            if not parent_category:
                raise HTTPException(
                    status_code=404, detail=f"父分类 {category_data.parent_id} 不存在"
                )

        db_category = await create_category(
            db=db,
            name=category_data.name,
            family_id=current_user.family_id,
            parent_id=category_data.parent_id,
            icon=category_data.icon,
            color=category_data.color,
            sort_order=category_data.sort_order,
        )
        created_categories.append(db_category)

    return created_categories


@router.get(
    "/categories", response_model=list[CategoryResponse], summary="获取分类列表"
)
async def get_categories_endpoint(
    parent_id: int | None = Query(
        None, description="父分类ID,不指定则获取顶级分类"
    ),
    skip: int = Query(0, ge=0, description="跳过记录数"),
    limit: int = Query(100, ge=1, le=1000, description="限制记录数"),
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取记账分类列表"""
    categories = await get_categories(
        db=db,
        family_id=current_user.family_id,
        parent_id=parent_id,
        skip=skip,
        limit=limit,
    )
    return categories


@router.get(
    "/categories/all", response_model=list[CategoryResponse], summary="获取所有分类"
)
async def get_all_categories_endpoint(
    skip: int = Query(0, ge=0, description="跳过记录数"),
    limit: int = Query(100, ge=1, le=1000, description="限制记录数"),
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取所有记账分类(包括子分类)"""
    categories = await get_all_categories(
        db=db, family_id=current_user.family_id, skip=skip, limit=limit
    )
    return categories


@router.get(
    "/categories/hierarchy",
    response_model=list[CategoryWithChildren],
    summary="获取分类层级结构",
)
async def get_categories_hierarchy_endpoint(
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取记账分类层级结构"""
    # 获取所有分类
    all_categories = await get_all_categories(db=db, family_id=current_user.family_id)

    # 构建层级结构
    category_dict = {cat.id: cat for cat in all_categories}
    root_categories = []

    for category in all_categories:
        if category.parent_id is None:
            root_categories.append(category)
        else:
            parent = category_dict.get(category.parent_id)
            if parent:
                if not hasattr(parent, "children"):
                    parent.children = []
                parent.children.append(category)

    # 转换为CategoryWithChildren格式
    def convert_to_hierarchy(category):
        children = []
        if hasattr(category, "children"):
            for child in category.children:
                children.append(convert_to_hierarchy(child))

        return CategoryWithChildren(
            id=category.id,
            name=category.name,
            parent_id=category.parent_id,
            icon=category.icon,
            color=category.color,
            sort_order=category.sort_order,
            family_id=category.family_id,
            is_system=category.is_system,
            created_at=category.created_at,
            updated_at=category.updated_at,
            children=children,
        )

    return [convert_to_hierarchy(cat) for cat in root_categories]


@router.get(
    "/categories/{category_id}", response_model=CategoryResponse, summary="获取分类详情"
)
async def get_category_endpoint(
    category_id: int,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取指定分类的详情"""
    category = await get_category_by_id(db, category_id, current_user.family_id)
    if not category:
        raise HTTPException(status_code=404, detail="分类不存在")
    return category


@router.put(
    "/categories/{category_id}", response_model=CategoryResponse, summary="更新分类"
)
async def update_category_endpoint(
    category_id: int,
    category_update: CategoryUpdate,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """更新记账分类"""
    # 如果指定了父分类,检查父分类是否存在
    if category_update.parent_id:
        parent_category = await get_category_by_id(
            db, category_update.parent_id, current_user.family_id
        )
        if not parent_category:
            raise HTTPException(status_code=404, detail="父分类不存在")

        # 不能将自己设为父分类
        if category_update.parent_id == category_id:
            raise HTTPException(status_code=400, detail="不能将自己设为父分类")

    category = await update_category(
        db=db,
        category_id=category_id,
        family_id=current_user.family_id,
        parent_id=category_update.parent_id,
        icon=category_update.icon,
        color=category_update.color,
        sort_order=category_update.sort_order,
    )

    if not category:
        raise HTTPException(status_code=404, detail="分类不存在")
    return category


@router.delete("/categories/{category_id}", status_code=204, summary="删除分类")
async def delete_category_endpoint(
    category_id: int,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """删除记账分类"""
    success = await delete_category(db, category_id, current_user.family_id)
    if not success:
        raise HTTPException(status_code=404, detail="分类不存在或无法删除")
    return {"message": "分类删除成功"}


# 账户相关的路由
@router.post(
    "/accounts", response_model=AccountResponse, status_code=201, summary="创建账户"
)
async def create_account_endpoint(
    account: AccountCreate,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """创建新的账户"""
    db_account = await create_account(
        db=db,
        name=account.name,
        account_type=account.account_type,
        family_id=current_user.family_id,
        initial_balance=account.initial_balance,
        icon=account.icon,
        color=account.color,
        description=account.description,
    )
    return db_account


@router.get("/accounts", response_model=list[AccountResponse], summary="获取账户列表")
async def get_accounts_endpoint(
    is_active: bool | None = Query(None, description="是否启用"),
    account_type: str | None = Query(None, description="账户类型"),
    skip: int = Query(0, ge=0, description="跳过记录数"),
    limit: int = Query(100, ge=1, le=1000, description="限制记录数"),
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取账户列表"""
    accounts = await get_accounts(
        db=db,
        family_id=current_user.family_id,
        is_active=is_active,
        account_type=account_type,
        skip=skip,
        limit=limit,
    )
    return accounts


@router.get(
    "/accounts/{account_id}", response_model=AccountResponse, summary="获取账户详情"
)
async def get_account_endpoint(
    account_id: int,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取指定账户的详情"""
    account = await get_account_by_id(db, account_id, current_user.family_id)
    if not account:
        raise HTTPException(status_code=404, detail="账户不存在")
    return account


@router.put(
    "/accounts/{account_id}", response_model=AccountResponse, summary="更新账户"
)
async def update_account_endpoint(
    account_id: int,
    account_update: AccountUpdate,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """更新账户信息"""
    account = await update_account(
        db=db,
        account_id=account_id,
        family_id=current_user.family_id,
        account_type=account_update.account_type,
        icon=account_update.icon,
        color=account_update.color,
        description=account_update.description,
        is_active=account_update.is_active,
    )

    if not account:
        raise HTTPException(status_code=404, detail="账户不存在")
    return account


@router.delete("/accounts/{account_id}", status_code=204, summary="删除账户")
async def delete_account_endpoint(
    account_id: int,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """删除账户(软删除)"""
    success = await delete_account(db, account_id, current_user.family_id)
    if not success:
        raise HTTPException(status_code=404, detail="账户不存在")
    return {"message": "账户删除成功"}


# 项目相关的路由
@router.post(
    "/projects", response_model=ProjectResponse, status_code=201, summary="创建项目"
)
async def create_project_endpoint(
    project: ProjectCreate,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """创建新的项目"""
    db_project = await create_project(
        db=db,
        name=project.name,
        family_id=current_user.family_id,
        description=project.description,
        start_date=project.start_date,
        end_date=project.end_date,
        budget=project.budget,
        color=project.color,
    )
    return db_project


@router.get("/projects", response_model=list[ProjectResponse], summary="获取项目列表")
async def get_projects_endpoint(
    is_active: bool | None = Query(None, description="是否启用"),
    skip: int = Query(0, ge=0, description="跳过记录数"),
    limit: int = Query(100, ge=1, le=1000, description="限制记录数"),
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取项目列表"""
    projects = await get_projects(
        db=db,
        family_id=current_user.family_id,
        is_active=is_active,
        skip=skip,
        limit=limit,
    )
    return projects


@router.get(
    "/projects/{project_id}", response_model=ProjectResponse, summary="获取项目详情"
)
async def get_project_endpoint(
    project_id: int,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取指定项目的详情"""
    project = await get_project_by_id(db, project_id, current_user.family_id)
    if not project:
        raise HTTPException(status_code=404, detail="项目不存在")
    return project


@router.put(
    "/projects/{project_id}", response_model=ProjectResponse, summary="更新项目"
)
async def update_project_endpoint(
    project_id: int,
    project_update: ProjectUpdate,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """更新项目信息"""
    project = await update_project(
        db=db,
        project_id=project_id,
        family_id=current_user.family_id,
        description=project_update.description,
        start_date=project_update.start_date,
        end_date=project_update.end_date,
        budget=project_update.budget,
        color=project_update.color,
        is_active=project_update.is_active,
    )

    if not project:
        raise HTTPException(status_code=404, detail="项目不存在")
    return project


@router.delete("/projects/{project_id}", status_code=204, summary="删除项目")
async def delete_project_endpoint(
    project_id: int,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """删除项目(软删除)"""
    success = await delete_project(db, project_id, current_user.family_id)
    if not success:
        raise HTTPException(status_code=404, detail="项目不存在")
    return {"message": "项目删除成功"}


# 交易记录相关的路由
@router.post(
    "/transactions",
    response_model=TransactionResponse,
    status_code=201,
    summary="创建交易记录",
)
async def create_transaction_endpoint(
    transaction: TransactionCreate,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """创建新的交易记录"""
    # 验证账户是否存在
    account = await get_account_by_id(
        db, transaction.account_id, current_user.family_id
    )
    if not account:
        raise HTTPException(status_code=404, detail="账户不存在")

    # 如果是转账,验证目标账户是否存在
    if transaction.to_account_id:
        to_account = await get_account_by_id(
            db, transaction.to_account_id, current_user.family_id
        )
        if not to_account:
            raise HTTPException(status_code=404, detail="目标账户不存在")

    # 如果指定了分类,验证分类是否存在
    if transaction.category_id:
        category = await get_category_by_id(
            db, transaction.category_id, current_user.family_id
        )
        if not category:
            raise HTTPException(status_code=404, detail="分类不存在")

    # 如果指定了项目,验证项目是否存在
    if transaction.project_id:
        project = await get_project_by_id(
            db, transaction.project_id, current_user.family_id
        )
        if not project:
            raise HTTPException(status_code=404, detail="项目不存在")

    # 如果没有指定 member_id,使用当前用户的 ID
    member_id = (
        transaction.member_id if transaction.member_id is not None else current_user.id
    )

    db_transaction = await create_transaction(
        db=db,
        transaction_type=transaction.transaction_type,
        amount=transaction.amount,
        account_id=transaction.account_id,
        member_id=member_id,
        family_id=current_user.family_id,
        transaction_date=transaction.transaction_date,
        category_id=transaction.category_id,
        to_account_id=transaction.to_account_id,
        project_id=transaction.project_id,
        description=transaction.description,
        is_recurring=transaction.is_recurring,
        recurring_pattern=transaction.recurring_pattern,
        tags=transaction.tags,
        receipt_image=transaction.receipt_image,
    )
    return db_transaction


@router.post(
    "/transactions/batch",
    response_model=list[TransactionResponse],
    status_code=201,
    summary="批量创建交易记录",
)
async def batch_create_transactions(
    batch_data: BatchTransactionCreate,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """批量创建交易记录"""
    created_transactions = []

    for transaction_data in batch_data.transactions:
        # 验证账户是否存在
        account = await get_account_by_id(
            db, transaction_data.account_id, current_user.family_id
        )
        if not account:
            raise HTTPException(
                status_code=404, detail=f"账户 {transaction_data.account_id} 不存在"
            )

        # 如果是转账,验证目标账户是否存在
        if transaction_data.to_account_id:
            to_account = await get_account_by_id(
                db, transaction_data.to_account_id, current_user.family_id
            )
            if not to_account:
                raise HTTPException(
                    status_code=404,
                    detail=f"目标账户 {transaction_data.to_account_id} 不存在",
                )

        # 如果指定了分类,验证分类是否存在
        if transaction_data.category_id:
            category = await get_category_by_id(
                db, transaction_data.category_id, current_user.family_id
            )
            if not category:
                raise HTTPException(
                    status_code=404,
                    detail=f"分类 {transaction_data.category_id} 不存在",
                )

        # 如果指定了项目,验证项目是否存在
        if transaction_data.project_id:
            project = await get_project_by_id(
                db, transaction_data.project_id, current_user.family_id
            )
            if not project:
                raise HTTPException(
                    status_code=404, detail=f"项目 {transaction_data.project_id} 不存在"
                )

        db_transaction = await create_transaction(
            db=db,
            transaction_type=transaction_data.transaction_type,
            amount=transaction_data.amount,
            account_id=transaction_data.account_id,
            member_id=transaction_data.member_id,
            family_id=current_user.family_id,
            transaction_date=transaction_data.transaction_date,
            category_id=transaction_data.category_id,
            to_account_id=transaction_data.to_account_id,
            project_id=transaction_data.project_id,
            description=transaction_data.description,
            is_recurring=transaction_data.is_recurring,
            recurring_pattern=transaction_data.recurring_pattern,
            tags=transaction_data.tags,
            receipt_image=transaction_data.receipt_image,
        )
        created_transactions.append(db_transaction)

    return created_transactions


@router.get("/transactions", response_model=TransactionList, summary="获取交易记录列表")
async def get_transactions_endpoint(
    transaction_type: TransactionType | None = Query(None, description="交易类型"),
    account_id: int | None = Query(None, description="账户ID"),
    category_id: int | None = Query(None, description="分类ID"),
    project_id: int | None = Query(None, description="项目ID"),
    member_id: int | None = Query(None, description="家庭成员ID"),
    start_date: date | None = Query(None, description="开始日期"),
    end_date: date | None = Query(None, description="结束日期"),
    search: str | None = Query(None, description="搜索关键词"),
    skip: int = Query(0, ge=0, description="跳过记录数"),
    limit: int = Query(100, ge=1, le=1000, description="限制记录数"),
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取交易记录列表"""
    transactions = await get_transactions(
        db=db,
        family_id=current_user.family_id,
        transaction_type=transaction_type,
        account_id=account_id,
        category_id=category_id,
        project_id=project_id,
        member_id=member_id,
        start_date=start_date,
        end_date=end_date,
        search=search,
        skip=skip,
        limit=limit,
    )

    # 获取总数
    total = len(transactions)

    return TransactionList(
        transactions=transactions, total=total, skip=skip, limit=limit
    )


@router.get(
    "/transactions/{transaction_id}",
    response_model=TransactionResponse,
    summary="获取交易记录详情",
)
async def get_transaction_endpoint(
    transaction_id: int,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取指定交易记录的详情"""
    transaction = await get_transaction_by_id(
        db, transaction_id, current_user.family_id
    )
    if not transaction:
        raise HTTPException(status_code=404, detail="交易记录不存在")
    return transaction


@router.put(
    "/transactions/{transaction_id}",
    response_model=TransactionResponse,
    summary="更新交易记录",
)
async def update_transaction_endpoint(
    transaction_id: int,
    transaction_update: TransactionUpdate,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """更新交易记录"""
    # 验证相关数据
    if transaction_update.account_id:
        account = await get_account_by_id(
            db, transaction_update.account_id, current_user.family_id
        )
        if not account:
            raise HTTPException(status_code=404, detail="账户不存在")

    if transaction_update.to_account_id:
        to_account = await get_account_by_id(
            db, transaction_update.to_account_id, current_user.family_id
        )
        if not to_account:
            raise HTTPException(status_code=404, detail="目标账户不存在")

    if transaction_update.category_id:
        category = await get_category_by_id(
            db, transaction_update.category_id, current_user.family_id
        )
        if not category:
            raise HTTPException(status_code=404, detail="分类不存在")

    if transaction_update.project_id:
        project = await get_project_by_id(
            db, transaction_update.project_id, current_user.family_id
        )
        if not project:
            raise HTTPException(status_code=404, detail="项目不存在")

    transaction = await update_transaction(
        db=db,
        transaction_id=transaction_id,
        family_id=current_user.family_id,
        transaction_type=transaction_update.transaction_type,
        amount=transaction_update.amount,
        account_id=transaction_update.account_id,
        to_account_id=transaction_update.to_account_id,
        category_id=transaction_update.category_id,
        project_id=transaction_update.project_id,
        member_id=transaction_update.member_id,
        transaction_date=transaction_update.transaction_date,
        description=transaction_update.description,
        is_recurring=transaction_update.is_recurring,
        recurring_pattern=transaction_update.recurring_pattern,
        tags=transaction_update.tags,
        receipt_image=transaction_update.receipt_image,
    )

    if not transaction:
        raise HTTPException(status_code=404, detail="交易记录不存在")
    return transaction


@router.delete(
    "/transactions/{transaction_id}", status_code=204, summary="删除交易记录"
)
async def delete_transaction_endpoint(
    transaction_id: int,
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """删除交易记录(软删除)"""
    success = await delete_transaction(db, transaction_id, current_user.family_id)
    if not success:
        raise HTTPException(status_code=404, detail="交易记录不存在")
    return {"message": "交易记录删除成功"}


# 统计相关的路由
@router.get(
    "/statistics/summary", response_model=TransactionSummary, summary="获取交易统计摘要"
)
async def get_statistics_summary_endpoint(
    start_date: date | None = Query(None, description="开始日期"),
    end_date: date | None = Query(None, description="结束日期"),
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取交易统计摘要"""
    summary = await get_transaction_summary(
        db=db,
        family_id=current_user.family_id,
        start_date=start_date,
        end_date=end_date,
    )
    return summary


@router.get(
    "/statistics/categories",
    response_model=list[CategorySummary],
    summary="获取分类统计",
)
async def get_category_statistics_endpoint(
    start_date: date | None = Query(None, description="开始日期"),
    end_date: date | None = Query(None, description="结束日期"),
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取分类统计"""
    stats = await get_category_statistics(
        db=db,
        family_id=current_user.family_id,
        start_date=start_date,
        end_date=end_date,
    )
    return stats


@router.get(
    "/statistics/accounts", response_model=list[AccountSummary], summary="获取账户统计"
)
async def get_account_statistics_endpoint(
    start_date: date | None = Query(None, description="开始日期"),
    end_date: date | None = Query(None, description="结束日期"),
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取账户统计"""
    stats = await get_account_statistics(
        db=db,
        family_id=current_user.family_id,
        start_date=start_date,
        end_date=end_date,
    )
    return stats


@router.get(
    "/statistics/projects", response_model=list[ProjectSummary], summary="获取项目统计"
)
async def get_project_statistics_endpoint(
    start_date: date | None = Query(None, description="开始日期"),
    end_date: date | None = Query(None, description="结束日期"),
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取项目统计"""
    stats = await get_project_statistics(
        db=db,
        family_id=current_user.family_id,
        start_date=start_date,
        end_date=end_date,
    )
    return stats


@router.get("/summary", response_model=TransactionSummary, summary="获取交易统计摘要")
async def get_summary_endpoint(
    start_date: date | None = Query(None, description="开始日期"),
    end_date: date | None = Query(None, description="结束日期"),
    current_user: FamilyMember = Depends(get_current_family_member),
    db: AsyncSession = Depends(get_db),
):
    """获取交易统计摘要"""
    summary = await get_transaction_summary(
        db=db,
        family_id=current_user.family_id,
        start_date=start_date,
        end_date=end_date,
    )
    return summary
